package bdqn.com.cui.dao.impl;

import bdqn.com.cui.dao.RecordDao;
import bdqn.com.cui.pojo.Record;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RecordDaoimpl extends BaseDao implements RecordDao {
    @Override
    public List<Record> getsenect(int form, int page) {
        Connection conn=getconn();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet=null;
        String sql="SELECT r.r_id,r.r_ordernumber,r.r_date,c.c_jiage,c.c_name,u.u_zhanghao FROM record r INNER JOIN commodity c ON c.c_id=r.c_jiage INNER JOIN `User` u ON u.u_id=r. u_name LIMIT ?,?";
        try {
            preparedStatement=conn.prepareStatement(sql);
            preparedStatement.setObject(1,form);
            preparedStatement.setObject(2,page);
            resultSet=preparedStatement.executeQuery();
            List<Record> list=new ArrayList<>();
            while (resultSet.next()){
                if (resultSet!=null){
                    Record record=new Record();
                    record.setId(resultSet.getInt(1));
                    record.setOrdernumber(resultSet.getString(2));
                    record.setDate(resultSet.getDate(3));
                    record.setJiage(resultSet.getInt(4));
                    record.setCommodity(resultSet.getString(5));
                    record.setUserzhanghao(resultSet.getString(6));
                    list.add(record);
                }
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            getclass(resultSet,preparedStatement,conn);
        }
    }

    @Override
    public int getcount() {
        Connection conn=getconn();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet=null;
        String sql="SELECT COUNT(1) FROM record";
        try {
            preparedStatement=conn.prepareStatement(sql);
            resultSet= preparedStatement.executeQuery();
            if (resultSet!=null){
                while (resultSet.next()){
                    return resultSet.getInt(1);
                }
            }
            return 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            getclass(resultSet,preparedStatement,conn);
        }
    }

    @Override
    public List<Record> getidserent(String id) {
        Connection conn=getconn();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet=null;
        String sql="SELECT r.r_id,r.r_ordernumber,r.r_date,c.c_jiage,c.c_name,u.u_zhanghao FROM record r INNER JOIN commodity c ON c.c_id=r.c_jiage INNER JOIN `User` u ON u.u_id=r. u_name WHERE r_ordernumber=?";
        try {
            preparedStatement=conn.prepareStatement(sql);
            preparedStatement.setObject(1,id);
            resultSet=preparedStatement.executeQuery();
            List<Record> list=new ArrayList<>();
            while (resultSet.next()){
                if (resultSet!=null){
                    Record record=new Record();
                    record.setId(resultSet.getInt(1));
                    record.setOrdernumber(resultSet.getString(2));
                    record.setDate(resultSet.getDate(3));
                    record.setJiage(resultSet.getInt(4));
                    record.setCommodity(resultSet.getString(5));
                    record.setUserzhanghao(resultSet.getString(6));
                    list.add(record);
                }
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            getclass(resultSet,preparedStatement,conn);
        }
    }

    @Override
    public int delete(String id) {
        String sql="DELETE FROM record WHERE r_id=?";
        Object[] obj= {id};
        return Crud(sql,obj);
    }

    @Override
    public int update(Record record) {
        return 0;
    }

    @Override
    public int Typeupdate(String type, String tj) {
        return 0;
    }
}
